/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-06-18     82752       the first version
 */
#ifndef SRC_DEVICE_DRIVER_LORA_AS32_H_
#define SRC_DEVICE_DRIVER_LORA_AS32_H_

#include <rtthread.h>
#include <rtdevice.h>
#include "common.h"

#define LORA_DEFAULT_ADDR            0
#define LORA_DEFAULT_CHANNEL         0x10

#define MD0_PIN_NAME                 "P001"
#define MD1_PIN_NAME                 "P003"
#define AUX_PIN_NAME                 "P000"

#define LORA_AS32_UART_NAME          "uart7"

#define LORA_REV_THREAD_STACK_SIZE   1024

#define LORA_PACK_MIN_SIZE           7           //no data, at last add a fcs
#define LORA_UART_HEADER1            0xFE
#define LORA_UART_HEADER2            0xFC
#define LORA_MAX_CHANNEL             0x1F
#define LORA_MAX_REV                 64
#define LORA_MAX_MQ                  10
#define LORA_AS32_UART_REV_TIMEOUT   100

typedef struct lora_as32_ack {
    uint8_t ack[8];
    uint8_t len;
} lora_as32_ack_t;

typedef enum {
    EVENT_OK_ACK = 1,
    EVENT_ERROR_ACK,
    EVENT_ARG_ACK
} lora_as32_ack_event;


typedef enum {
    POS_LEN = 0,
    POS_MSG_START
} lora_uart_pack_pos_t;

typedef enum {
    STATE_HEARDER1,
    STATE_HEARDER2,
    STATE_LEN,
    STATE_MSG,
    STATE_FCS
} lora_uart_state_t;

enum lora_as32_mode {
    MODE_NORMAL,
    MODE_WAKE,
    MODE_POWERLESS,
    MODE_SLEEP
};

enum lora_as32_control_cmd {
    SET_MODE,
    SET_ADDR,
    RESET,
    GET_ARGS
};

typedef struct lora_as32_set {

    uint8_t lora_arg[6];

    struct rt_device             parent;

    struct rt_semaphore          lora_doing_sem;
    struct rt_semaphore          lora_uart_rev_sem;
    rt_device_t                  serial;

    volatile int cur_lora_mode;

    rt_uint32_t aux_pin_num;
    rt_uint32_t md0_pin_num;
    rt_uint32_t md1_pin_num;

    struct rt_thread lora_rev_thread;

    uint8_t msg_pool[512];
    struct rt_messagequeue rt_mq;
    struct rt_event ack_event;

} *lora_as32_set_t;

#endif /* SRC_DEVICE_DRIVER_LORA_AS32_H_ */
